package ex1.game.players;

import ex1.game.Action;
import ex1.game.Player;
import ex1.game.PrisonersDilemmaGame;
import ex1.utils.Utils;

public class TruePeaceMaker implements Player {

	private double _makePeace = 0.1;
	private Action[] _history = new Action[2];
	
	public TruePeaceMaker()
	{
		_history[0] = null;
		_history[1] = null;
	}
	
	@Override
	public Action playGame(PrisonersDilemmaGame game,
			Action otherPlayerLastAction) {
		
		if (otherPlayerLastAction == null)
		{
			_history[0] = null;
			_history[1] = null;
		}
		
		_history[1] = _history[0];
		_history[0] = otherPlayerLastAction;
		
		if((_history[0] == Action.Deffect) && (_history[1] == Action.Deffect))
		{
			if(Utils.RandomGenerator().nextDouble() < _makePeace)
			{
				return Action.Cooperate;
			}
			else
			{
				return Action.Deffect;
			}
		}
		
		return Action.Cooperate;
	}
	
	@Override
	public String toString()
	{
		return "TruePeaceMaker";
	}

}
